package meijuli.com.join51.business.serviceprovide;

import com.jhx.common.http.ApiManager;
import com.jhx.common.http.HttpRequest;
import com.jhx.common.http.HttpResponse;
import com.jhx.common.http.ResponseCallback;
import meijuli.com.join51.business.model.serviceprovide.OrderData;
import meijuli.com.join51.business.model.serviceprovide.OrderPayData;
import meijuli.com.join51.business.model.serviceprovide.PayInfo;
import meijuli.com.join51.business.model.serviceprovide.ServiceProviderPayStatus;
import meijuli.com.join51.business.model.serviceprovide.ServiceShopData;

import meijuli.com.join51.business.service.ApiService;
import retrofit2.Call;

/**
 * 申请品牌服务商实现类
 *
 * @author Wizong 2017-05-19 13:09
 */
class ServiceProvidePresenterImpl implements Contract.ServiceProviderPresenter {
    private ServiceProvideFragment view;
    private ApiManager manager = ApiManager.get();

    public ServiceProvidePresenterImpl(ServiceProvideFragment view) {
        this.view = view;
    }

    @Override
    public void getPayList() {
        Call<HttpResponse<PayInfo>> call = manager.getApiService(true, ApiService.class).getPayList("5");
        HttpRequest.getRequest().autoRecycle(view).start(call, new ResponseCallback<PayInfo>() {
            @Override
            public void onSuccess(String url, PayInfo data) {
                view.payList(data);
            }

            @Override
            public void onError(String url, int errorCode, String errorMsg) {
                view.error(errorMsg);
            }
        });
    }

    @Override
    public void getServiceShopList() {
        Call<HttpResponse<ServiceShopData>> call = manager.getApiService(true, ApiService.class).getServiceShopList();
        HttpRequest.getRequest().autoRecycle(view).start(call, new ResponseCallback<ServiceShopData>() {
            @Override
            public void onSuccess(String url, ServiceShopData data) {
                if (data != null && data.getList() != null && data.getList().size() > 0) {
                    view.serviceShopList(data.getList());
                }
            }

            @Override
            public void onError(String url, int errorCode, String errorMsg) {
                view.error(errorMsg);
            }
        });
    }

    @Override
    public void serviceShopSubOrder(String spSettingId, String businessPhone) {
        Call<HttpResponse<OrderData>> call = manager.getApiService(true, ApiService.class)
                .serviceShopSubOrder(spSettingId, businessPhone);
        HttpRequest.getRequest().autoRecycle(view).start(call, new ResponseCallback<OrderData>(){

            @Override
            public void onSuccess(String url, OrderData data) {
                if (data != null) {
                    view.serviceShopOrder(data);
                }
            }

            @Override
            public void onError(String url, int errorCode, String errorMsg) {
                view.error(errorMsg);
            }
        });
    }

    @Override
    public void getOrderPay(String orderId, String payType) {
        Call<HttpResponse<OrderPayData>> call =
                manager.getApiService(true, ApiService.class).getOrderPay(orderId, payType);
        HttpRequest.getRequest().autoRecycle(view).start(call, new ResponseCallback<OrderPayData>() {

            @Override
            public void onSuccess(String url, OrderPayData data) {
                if (data != null) {
                    view.orderPay(data);
                }
            }

            @Override
            public void onError(String url, int errorCode, String errorMsg) {
                view.error(errorMsg);
            }
        });
    }

    @Override
    public void checkRecharge(String outTradeNo) {
        Call<HttpResponse<ServiceProviderPayStatus>> call = manager.getApiService(true, ApiService.class)
                .getServiceProviderPayStatus(outTradeNo);
        HttpRequest.getRequest().autoRecycle(view).start(call, new ResponseCallback<ServiceProviderPayStatus>() {

            @Override
            public void onSuccess(String url, ServiceProviderPayStatus data) {
                if (data != null) {
                    view.serviceProviderPayStatus(data);
                }
            }

            @Override
            public void onError(String url, int errorCode, String errorMsg) {
                view.error(errorMsg);
            }
        });
    }
}
